<?php
class FeedbackForm extends Form
{
    const FIELD_NAME           = 'name';
    const FIELD_EMAIL          = 'email';
    const FIELD_MESSAGE        = 'message';
    
    protected static $errorMap = array(
        'name_required'         => array('message' => 'Please enter your name', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Name'),
        'email_required'        => array('message' => 'Please enter a valid e-mail address', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Email'),
        'email_invalid'         => array('message' => 'Please enter existing e-mail address', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Email'),
        'message_required'      => array('message' => 'Please enter a message', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Message')
      );

    protected static $publicKeyFieldName  = 'public_key';
    protected static $privateKeyFieldName = 'captcha';
    protected static $captchaSecret = "A very, nano very secret string which is used to generate a md5-key!";      
      
    protected function GetTemplatePath()
    {
        return 'include/forms/feedback_form.tpl';
    }

    public function Setup()
    {
        $this->SetFields( array(
            self::FIELD_NAME, self::FIELD_EMAIL, self::FIELD_MESSAGE ) );
    }
    
    private function GetCaptchaCode($publicKey)
    {
        return strtolower(substr(md5(md5(self::$captchaSecret) . $publicKey), 16 - 5 / 2, 4));
    }

    protected function Validate()
    {        
        $name = preg_replace('/\s+/', ' ', $this->GetParamValue(self::FIELD_NAME, ''));
        if (!empty($name))
        {
            $this->SetValue(self::FIELD_NAME, $name);
        }
        else
        {
            $this->SetError(self::FIELD_NAME, self::$errorMap['name_required']);
        }

        $email = filter_var($this->GetParamValue(self::FIELD_EMAIL), FILTER_VALIDATE_EMAIL);
        if ($email !== false)
        {                        
            $isValidMail = Util::checkMXRecord($email);
            if ($isValidMail)
            {
                $this->SetValue(self::FIELD_EMAIL, $email);
            }
            else
            {
                $this->SetError(self::FIELD_EMAIL, self::$errorMap['email_invalid']);
            }
        }
        else
        {
            $this->SetError(self::FIELD_EMAIL, self::$errorMap['email_required']);
        }

        $message = trim($this->GetParamValue(self::FIELD_MESSAGE));
        if ($message === '')
        {
            $this->SetError(self::FIELD_MESSAGE, self::$errorMap['message_required']);
        }
        else
        {
            $this->SetValue(self::FIELD_MESSAGE, $message);
        }
        
        $this->ValidateCaptcha();
    }
    
    private function ValidateCaptcha()
    {
        $privateKey = strtolower( $this->GetParamValue( self::$privateKeyFieldName ) );
 
        $publicKey  = Session::GetAndClearValue( self::$publicKeyFieldName );

        if ( !isset($publicKey) || empty($publicKey) || $this->GetCaptchaCode($publicKey) !=  $privateKey )
        {
             $this->SetError(self::$privateKeyFieldName, self::$captchaInvalidMessage);
        }
        else
        {
             $this->SetValue(self::$publicKeyFieldName, $publicKey);
             $this->SetValue(self::$privateKeyFieldName, $privateKey);
        }
    }
}
?>